﻿@{
    ViewBag.Title = "Search";
}

<script src="@Url.Content("~/Scripts/LAF/jquery.tmpl.min.js")" type="text/javascript"></script>

<script src="@Url.Content("~/Scripts/LAF/json2.js")" type="text/javascript"></script>



<script type="text/javascript">
    jQuery(document).ready(function () {

        try {
            Search('@ViewBag.ABC', 1, 5);
        }
        catch (err) {
            alert("exeption")
        }
    });

    function Search(topicToSearch, pageId, pageSize) {
        var params = {
            "topicToSearch": topicToSearch,
            "pageId": pageId,
            "pageSize": pageSize
        };
        var jsonParams = JSON.stringify(params);
        jQuery.ajax({
            type: "POST",
            data: jsonParams,
            url: '/Home/Search',
            dataType: "html",
            contentType: "application/json",
            success: function (jsonResult) {
                var result = jQuery.parseJSON(jsonResult);
                //var result = eval('('+jsonResult+')');
                var markup = '<div><p><a class="title" href="/Home/ShowTopic/${ID}" >${Title}</a><br/><b>${UserPost} </b>đăng vào ${TimePost}<br/>Vào lúc ${Time} tại ${Location}<br/><b>${Content}</b></p></div>';
                jQuery.template("topicForm", markup);
                jQuery(contain).empty();
                jQuery.tmpl("topicForm", result.Topics).appendTo(contain);
                //Show Paging
                var temp = (result.TopicCount % result.PageSize) > 0 ? 1 : 0;
                var pageCount = (result.TopicCount - result.TopicCount % result.PageSize) / result.PageSize + temp;
                var firstCommand = result.PageId > 1 ? 'onclick=Search(' + @ViewBag.ABC + ',1, ' + result.PageSize + ')' : '';
                var prevCommand = result.PageId > 1 ? 'onclick=Search(' + @ViewBag.ABC + ',' + (result.PageId - 1) + ', ' + result.PageSize + ')' : '';
                var nextCommand = result.PageId < pageCount ? 'onclick=Search(' + @ViewBag.ABC + ',' + (result.PageId + 1) + ', ' + result.PageSize + ')' : '';
                var lastCommand = result.PageId < pageCount ? 'onclick=Search(' + @ViewBag.ABC + ',' + pageCount + ', ' + result.PageSize + ')' : '';


                jQuery(containPage).empty();
                jQuery(containPage).append('Trang ' + result.PageId + '/' + pageCount + ' ');
                jQuery(containPage).append('<a href="#" ' + firstCommand + '>Trang đầu</a> | ');
                jQuery(containPage).append('<a href="#" ' + prevCommand + '>Trước</a> | ');
                jQuery(containPage).append('<a href="#" ' + nextCommand + '>Tiếp</a> | ');
                jQuery(containPage).append('<a href="#" ' + lastCommand + '>Trang cuối</a> ');

            },
            error: function () {
                alert("Không tải được nội dung!");
            }
        });
    };
    function showTopic(id) {

    }
</script>
<div class="colpost" id="left_col">
    <h1 class="page_title">Bạn có thể tham khảo các bài sau</h1>
    <hr>
    <div id="contain">
    </div>

    <div id="containPage">
    </div>
</div>
